import java.io.FileInputStream;
import java.lang.reflect.Method;

public class CusJvmLoader {

    static class MyClassLoader extends ClassLoader {
        private String classPath;
        public MyClassLoader(String classPath) {
            this.classPath = classPath;
        }
        private byte[] loadByte(String name) throws Exception {
            name = name.replaceAll("\\.", "/");
            FileInputStream fis = new FileInputStream(classPath + "/" + name + ".class");
            int len = fis.available();
            byte[] data = new byte[len];
            fis.read(data);
            fis.close();
            return data;
        }

        protected Class<?> findClass(String name) throws ClassNotFoundException{
            try {
                byte[] data = loadByte(name);
                return defineClass(name, data, 0, data.length);
            }catch (Exception e) {
                e.printStackTrace();
                throw new ClassNotFoundException();
            }
        }
    }


    public static void main(String[] args) throws Exception {

        getCustomClassloaderTest();
    }

    public static void getCustomClassloaderTest() throws Exception{
        MyClassLoader classLoader = new MyClassLoader("F:/Temp");
        Class clazz = classLoader.loadClass("com.meng.jvm.User2");
        Object obj = clazz.newInstance();
        Method method = clazz.getDeclaredMethod("sout", null);
        method.invoke(obj, null);
        System.out.println(clazz.getClassLoader().getClass().getName());
    }

    public static void getClassloaderTest(){
        System.out.println(String.class.getClassLoader());
        System.out.println(com.sun.crypto.provider.DESKeyFactory.class.getClassLoader().getClass().getName());
        System.out.println(CusJvmLoader.class.getClassLoader().getClass().getName());
        System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());
    }
}
